/*
 * Copyright (C) 2021 The Chinese Software International Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tonyodev.fetch2.util;

import ohos.aafwk.content.Intent;
import ohos.app.Context;

import com.tonyodev.fetch2.DownloadNotification;
import com.tonyodev.fetch2.Fetch;
import com.tonyodev.fetch2.FetchNotificationManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * description NotificationUtils
 *
 * @author liuluming
 * @since 2021-05-24
 */
public final class NotificationUtils {
    /**
     * onDownloadNotificationActionTriggered
     *
     * @param context context
     * @param intent intent
     * @param fetchNotificationManager fetchNotificationManager
     */
    public static final void onDownloadNotificationActionTriggered(Context context, Intent intent, FetchNotificationManager fetchNotificationManager) {
        if (context != null && intent != null) {
            String namespace = intent.getStringParam("com.tonyodev.fetch2.extra.NAMESPACE");
            int downloadId = intent.getIntParam("com.tonyodev.fetch2.extra.DOWNLOAD_ID", -1);
            int actionType = intent.getIntParam("com.tonyodev.fetch2.extra.ACTION_TYPE", -1);
            int notificationId = intent.getIntParam("com.tonyodev.fetch2.extra.NOTIFICATION_ID", -1);
            int notificationGroupId = intent.getIntParam("com.tonyodev.fetch2.extra.NOTIFICATION_GROUP_ID", -1);
            boolean isGroupAction = intent.getBooleanParam("com.tonyodev.fetch2.extra.GROUP_ACTION", false);
            ArrayList list = intent.getSequenceableArrayListParam("con.tonyodev.fetch2.extra.DOWNLOAD_NOTIFICATIONS");
            List<DownloadNotification> downloadNotifications = list != null ? list : Collections.emptyList();
            if (!isGroupAction) {
                CharSequence charSequence = namespace;
                if (charSequence != null && charSequence.length() != 0 && downloadId != -1 && actionType != -1) {
                    Fetch fetch = fetchNotificationManager.getFetchInstanceForNamespace(namespace);
                    if (!fetch.isClosed()) {
                        switch (actionType) {
                            case 0:
                                fetch.pause(downloadId);
                                break;
                            case 1:
                                fetch.resume(downloadId);
                                break;
                            case 2:
                                fetch.delete(downloadId);
                                break;
                            case 3:
                                fetch.cancel(downloadId);
                                break;
                            case 4:
                                fetch.retry(downloadId);
                                break;
                        }
                    }
                }
            } else if (notificationGroupId != -1) {
                if (notificationGroupId != -1 && !downloadNotifications.isEmpty()) {
                    Map<String, List<DownloadNotification>> downloadByNames = new LinkedHashMap<>();
                    for (DownloadNotification downloadNotification : downloadNotifications) {
                        String key = downloadNotification.getNamespace();
                        List<DownloadNotification> notifications = downloadByNames.get(key);
                        if (notifications == null) {
                            notifications = new ArrayList<>();
                        }
                        notifications.add(downloadNotification);
                        downloadByNames.put(key, notifications);
                    }

                    for (String key : downloadByNames.keySet()) {
                        List<DownloadNotification> notifications = downloadByNames.get(key);
                        List<Integer> downloadIds = new ArrayList<>();
                        for (DownloadNotification notification : notifications) {
                            downloadIds.add(notification.getNotificationId());
                        }
                        Fetch fetch = fetchNotificationManager.getFetchInstanceForNamespace(key);
                        if (!fetch.isClosed()) {
                            switch (actionType) {
                                case 6:
                                    fetch.pause(downloadIds);
                                    break;
                                case 7:
                                    fetch.resume(downloadIds);
                                    break;
                                case 8:
                                    fetch.cancel(downloadIds);
                                    break;
                                case 9:
                                    fetch.delete(downloadIds);
                                    break;
                                case 10:
                                    fetch.retry(downloadIds);
                                    break;
                            }
                        }
                    }
                }
            }
        }

    }
}
